﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Collections;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_DAL;
using System.Data;

namespace STHPMS1.BLL.B_Bus
{
    public class Dp_Dailymain_CostBus : BllBase, IDp_Dailymain_CostBill
    {
        public void Save<T>(T entity)
            where T : class ,IDp_Dailymain_Cost
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDp_Dailymain_Cost entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDp_Dailymain_Cost
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }

         /// <summary>
        /// 通过决策路段获取基础路段信息
        /// </summary>
        /// <param name="mainsegid"></param>
        /// <returns></returns>
        public IB_Invntory GetB_Invntory(string mainsegid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Dailymain_Cost.GetB_Invntory(mainsegid);
        }
        /// <summary>
        /// 通过决策路段获取基础路段信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="segid_main">养护类型</param>
        /// <returns></returns>
        public List<T> GetB_Invntory<T>(string rdwayid, int segid_main) where T : class,IB_Invntory
        {
            return this.Scene.Dal.Sthpms1.Dp_Dailymain_Cost.GetB_Invntory<T>(rdwayid, segid_main);
        }


        /// <summary>
        /// 日常养护同步数据
        /// </summary>
        /// <param name="roadnetid">路网ID</param>
        /// <param name="analy_year">规划年度</param>
        public void Insert(string anniname,string analy_year)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Delete<Dp_Dailymain_Cost>(p => p.Anni_Name == anniname && p.Analy_Year == analy_year);
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();


            //路网决策路段表中获取数据
            Expression<Func<Dn_Divi_Planning, bool>> exp = null;
            if (analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname && pre.Divi_Time == analy_year.ToString() && pre.Segid_Main == 12000;
            List<Dn_Divi_Planning> list = this.Scene.Bll.Dn_Divi_Planning.Get<Dn_Divi_Planning>(exp, null).ToList<Dn_Divi_Planning>();


            foreach (Dn_Divi_Planning info in list)
            {
                Dp_Dailymain_Cost dminfo = new Dp_Dailymain_Cost();
                dminfo.Id = serv.NewGuid(); //ID
                dminfo.Anni_Name =info.Anni_Name; //资金模型名称
                dminfo.RoadnetId = info.RoadnetId; //路网ID
                dminfo.Rdwayid = info.Rdwayid; //路线编号
                dminfo.Mainsegid = info.Mainsegid; //决策路段ID


                //dminfo.Rd_Class = info.Rdclass_Dictid; //技术等级
                //dminfo.Pvmttype = info.Pvmttype; //路面类型

                IB_Invntory b_invntoryInfo = GetB_Invntory(info.Mainsegid);

                //通过基础路段表中获取信息
                dminfo.Rd_Class = b_invntoryInfo.Rdclass_Dictid; //技术等级
                dminfo.Pvmttype = b_invntoryInfo.Pvmttype_Dictid; //路面类型


                double lenght = Convert.ToDouble(info.Endmp - info.Beginmp);

                //通过决策路段获取基础路段信息
                //IB_Invntory b_invntoryInfo = GetB_Invntory(info.Mainsegid);
                int Year = (DateTime.Now - Convert.ToDateTime(b_invntoryInfo.Begindate)).Days / 365;
                double cost = dao.Df_Dailymian_Cost.GetDaily_Unitcost(Convert.ToInt32(b_invntoryInfo.Pvmttype_Dictid), b_invntoryInfo.Rdclass_Dictid.ToString(), Year); //通车年限？？？
                dminfo.Roadlenght = lenght;
                dminfo.Cost = lenght/1000 * cost;  //路段日常养护总费用
                dminfo.Analy_Year = info.Divi_Time; //分析年度：决策路段表中规划年度
                dao.Save<Dp_Dailymain_Cost>(dminfo);  //保存 日常养护费用表

                //更新 路网决策路段表 方案资金,养护年份
                dao.Update<Dn_Divi_Planning>(new { Project_Id = dminfo.Id, Project_Name = "", Cost = dminfo.Cost, Main_Year = info.Divi_Time }, p => p.Id == info.Id);


            }

        }
    }
}
